package com.walnut.lom_system.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.walnut.lom_system.common.lang.Const;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author walnut
 * @version 1.0
 * @className MyMetaObjectHandler
 * @description TODO
 * @date 2022/1/21 14:41
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时的填充策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill==>");
        //setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) {
        //创建时间自动填充
        this.setFieldValByName("createTime",new Date(),metaObject);
        //最近更新时间自动填充
        this.setFieldValByName("lastEditTime",new Date(),metaObject);
        //状态位自动填充
        this.setFieldValByName("deleted",0,metaObject);

        this.setFieldValByName("state", Const.USER_ON_USE,metaObject);
    }

    /**
     * 更新时的填充策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill==>");
        this.setFieldValByName("lastEditTime",new Date(),metaObject);
    }
}
